import React from 'react'
import PropTypes from 'prop-types'
import {Modal, Form, Input, Select, Avatar, Button, message} from 'antd';
import api from "../../../../api/api";
import axios from "axios";

const {Option} = Select;
const FormItem = Form.Item;

@Form.create()
class Model extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            visible: false,
            param: {
                tenantCompanyName: '',
                tenantName: '',
                tenantPassword: '',
                tenantId: '',
                tenantGid:''
            }
        };
    }

    componentDidMount() {
        console.log("dialog初始化==", this.props);
        this.props.onRef(this)
    }

    handleOk = () => {
        this.props.form.validateFields((error, values) => {
            if (error != null) {
                return;
            }
            let {param} = this.state
            console.log("values", values);
            console.log("param", param)
            param.tenantCompanyName = values.tenantCompanyName
            param.tenantName = values.tenantName
            param.tenantPassword = values.tenantPassword
            param.tenantGid=values.tenantGid
            this.setState({param}, () => {
                this.tenantData()
            })
        })
    }

    setBaseInfo = () => {
        let data = this.props.data.gridList
        const {form} = this.props;
        console.log("data", data)
        console.log("form", form)
        Object.keys(form.getFieldsValue()).forEach(key => {
            const obj = {};
            if (data[key]) {
                obj[key] = data[key] || null;
            }
            form.setFieldsValue(obj);
        });
    };

    //清空form表单数据
    clearData() {
        this.props.form.resetFields();
    }

    //初始化编辑数据
    initEditData = () => {
        this.clearData()
        this.setBaseInfo()
    }

    tenantData() {
        let url = ""
        if (this.props.data.type === 2) {
            url = api.api_URL + "/admin/tenant/updateTenantInfo"
            let {param} = this.state
            param.tenantId = this.props.data.gridList.tenantId
            this.setState({param}, () => {
                this.addTenant(url)
            })
        } else {
            url = api.api_URL + "/admin/tenant/addTenantInfo"
            let {param} = this.state
            param.tenantId = ''
            this.setState({param}, () => {
                this.addTenant(url)
            })
        }
    }

    //添加
    addTenant(url) {
        const that = this;
        axios({
            method: 'POST',
            url: url,
            data: JSON.stringify(that.state.param),
            headers: {
                "Authorization": localStorage.getItem('token'),
                "Content-Type": "application/json;charset=UTF-8"
            },
        }).then(function (response) {
            if (response.data.code != 1) {
                message.error({content: "失败，请重新尝试 ！"})
                return;
            }
            message.success({content: "成功！"})
            that.handleCancel()
        })
            .catch(function (error) {
                message.error({content: "失败，请重新尝试 ！"})
                console.log(error);
            })
    }

    handleCancel = e => {
        console.log(e);
        this.props.handleCancel()
    };

    render() {
        //getFieldError  验证表单需要加
        const {getFieldDecorator, getFieldError} = this.props.form
        return (
            <div>
                <Modal
                    title={this.props.data.title}
                    visible={this.props.data.visible}
                    onOk={this.handleOk}
                    onCancel={this.handleCancel}
                >
                    <Form name="control-ref" autoComplete="off" ref={this.formRef}
                          labelCol={{span: 4}}
                          wrapperCol={{span: 14}}
                    >
                        <FormItem label="gid" name="tenantGid">
                            {getFieldDecorator("tenantGid", {
                                rules: [
                                    {
                                        required: true,
                                        message: "请输入gid"
                                    }
                                ]
                            })(
                                <Input/>
                            )}
                        </FormItem>
                        <FormItem label="账号" name="tenantName">
                            {getFieldDecorator("tenantName", {
                                rules: [
                                    {
                                        required: true,
                                        message: "请输入账号"
                                    }
                                ]
                            })(
                                <Input/>
                            )}
                        </FormItem>
                        <FormItem label="密码" name="tenantPassword">
                            {getFieldDecorator("tenantPassword", {
                                rules: [
                                    {
                                        required: true,
                                        message: "请输入密码"
                                    }
                                ]
                            })(
                                <Input.Password/>
                            )}
                        </FormItem>
                        <FormItem label="公司" name="tenantCompanyName">
                            {getFieldDecorator("tenantCompanyName", {
                                rules: [
                                    {
                                        required: true,
                                        message: "请输入公司"
                                    }
                                ]
                            })(
                                <Input/>
                            )}
                        </FormItem>
                    </Form>
                </Modal>
            </div>
        );
    }
}

export default Model